<?php
class AttachExpenseExternalAction extends CAction {
	public function run(){
		$model = new MaterialExpenseAttachExternal;
		if (isset($_POST['MaterialExpenseAttachExternal'])) {
			$model->attributes = $_POST['MaterialExpenseAttachExternal'];
			$successCount=0;
			$failCount=0;
			if ($model->validate()){
				$expenses=MaterialExpense::model()->findAllByPk($model->materialExpenseIds);
				foreach ($expenses as $expense) {
					$expense->setScenario('attachExternalToken');
					$expense->externalTokenDate = $model->externalTokenDate;
					$expense->externalTokenNumber = $model->externalTokenNumber;
					if($expense->save()){
						$successCount++;
					}else{
						$failCount++;
					}
				}
				$message='凭证'.$model->externalTokenDate .'#'.$model->externalTokenNumber.' 成功附加 '.$successCount.' 张报销单'.($failCount>0?' 失败 '.$failCount.' 张':'');
				Yii::app()->user->setFlash('attachExpenseExternal', $message);
			}
		}
		$this->getController()->render('attachExpenseExternal', array(
				'model' => $model,
				'message' => $message,
			));
	}
}